import unittest

from pyramid.configuration import Configurator
from pyramid import testing
from pyramid.view import render_view
from ampersands.omero.models import IOMERO, IOMEROProject, \
    IOMERODataset, IOMEROImage

class ViewTests(unittest.TestCase):
    def setUp(self):
        self.config = Configurator()
        self.config.begin()
        self.config.load_zcml('ampersands.omero:configure.zcml')
        self.root = testing.DummyModel('', None, IOMERO)
        self.root['101'] = testing.DummyModel('test_project', self.root,
                                              IOMEROProject,
                                              name="test_project",
                                              OMERO_CLASS="Project",
                                              id="101",
                                              description="test desciption.")
        p = self.root['101']
        p['201'] = testing.DummyModel('test_dataset', self.root,
                                      IOMERODataset,
                                      name="test_dataset",
                                      OMERO_CLASS="Dataset",
                                      id="201",
                                      description="test desciption.")
        d = self.root['101']['201']
        d['301'] = testing.DummyModel('test_image', self.root,
                                      IOMEROImage,
                                      name="test_image",
                                      OMERO_CLASS="Image",
                                      id="301",
                                      description="test desciption.")

    def tearDown(self):
        self.config.end()

    def test_list_omero_projects(self):
        request = testing.DummyRequest()
        info = render_view(self.root, request)
        from xml.dom.minidom import parseString
        dom = parseString(info)
        items = dom.getElementsByTagName('item')
        for i in items:
            self.assertTrue(i.hasAttribute('id'))
            self.assertTrue(i.hasAttribute('name'))
            self.assertTrue(i.hasAttribute('url'))
            self.assertTrue(i.hasAttribute('type'))
            self.assertTrue(i.getAttribute('type') == 'Project')

    def test_list_omero_datasets(self):
        request = testing.DummyRequest()
        info = render_view(self.root['101'], request)
        from xml.dom.minidom import parseString
        dom = parseString(info)
        items = dom.getElementsByTagName('item')
        for i in items:
            self.assertTrue(i.hasAttribute('id'))
            self.assertTrue(i.hasAttribute('name'))
            self.assertTrue(i.hasAttribute('url'))
            self.assertTrue(i.hasAttribute('type'))
            self.assertTrue(i.getAttribute('type') == 'Dataset')

    def test_list_omero_images(self):
        request = testing.DummyRequest()
        info = render_view(self.root['101']['201'], request)
        from xml.dom.minidom import parseString
        dom = parseString(info)
        items = dom.getElementsByTagName('item')
        for i in items:
            self.assertTrue(i.hasAttribute('id'))
            self.assertTrue(i.hasAttribute('name'))
            self.assertTrue(i.hasAttribute('url'))
            self.assertTrue(i.hasAttribute('type'))
            self.assertTrue(i.getAttribute('type') == 'Image')
